package com.example.boardroom.serial.table.manager;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.boardroom.serial.table.ModbusRtu.ModbusUtils;
import com.example.boardroom.serial.table.ModbusRtu.ParseSystemUtil;

public class DAM3200Manager {
    //hint: Updates were rejected because the tip of your current branch is behind
//hint: its remote counterpart. Integrate the remote changes (e.g.
//hint: 'git pull ...') before pushing again.
//hint: See the 'Note about fast-forwards' in 'git push --help' for details.
    private final String port = "01";
    private final String address = "0000";//表通讯地址
    private final String functionCode = "01";//功能码


    /**
     * @return 查询32位继电器状态(32)
     */
    public String queryDAM3200() {
        return "0101000000203DD2";
    }//应答帧:01 01 04 80 08 01 80 53 E3


    /**
     * @return 查询32位A组继电器状态(A组16位)
     */
    public String queryDAM3200A() {
        return "01 01 00 00 00 10 3D C6";
    }//应答帧:01 01 02 00 00 B9 FC

    /**
     * @return 查询32位B组继电器状态(B组16位)
     */
    public String queryDAM3200B() {
        return "01 01 00 10 00 10 3C 03";
    }//应答帧:01 01 02 00 00 B9 FC
    //解答模型
    //10000000000010000000 0001 1000 0000
    //1 0 0 0  0 0 0 0  0 0 0 0  1 0 0 0   0  0  0  0   0  0  0  1   1  0  0  0    0  0  0  0
    //8 7 6 5  4 3 2 1  16151413 1211109  24 23 22 21  20  19 18 17  32 31 30 29   28 27 26 25

    /**
     * 翻译32路继电器
     * 依次判断返回数据的每一位是1或0  1代表开,0代表关
     * 如果返回仅一位0,代表发生错误
     *
     * @param res 原始16位应答帧
     * @return 依次判断返回数据的每一位是1或0  1代表开,0代表关
     */
    //01 01 04 80 08 01 80 53 E3
    public String translateDAM3200(String res) {
        String frame = res.replaceAll(" ", "");
        String port = frame.substring(0, 2);
        if (frame.length()!=18) {
            LogUtils.dTag("lufeidebug","32路解析错误3");
            return "0";
        }
        if (!port.equals(this.port)) {
            LogUtils.dTag("lufeidebug","32路解析错误2");
            return "0";
        }
        String s = frame.substring(0, 6);
        if (!s.equals("010104")) {
            ToastUtils.showShort("32路解析错误1");
            return "0";
        }
        String data = frame.substring(6, 14);
        String aByte = ParseSystemUtil.hexStringToByte(data);
        StringBuilder builder = new StringBuilder(32);
        if (aByte.equals("0")) {
            builder.append("00000000000000000000000000000000");
        } else {
            builder.append(aByte);
            while (builder.length() < 32) {
                builder.insert(0, "0");
            }
        }

        return String.valueOf(builder);
    }


    /**
     * 打开第num个继电器  从0开始到31结束
     *
     * @param num 继电器从0开始数
     * @return 闻讯帧
     */
    public String openThisDAM(int num) {
        return ModbusUtils.splicing(port, "05", ParseSystemUtil.decToHex(num), "FF00", false);
    }

    /**
     * 关闭第num个继电器  从0开始到31结束
     *
     * @param num 继电器从0开始数
     * @return 闻讯帧
     */
    public String closeThisDAM(int num) {
        return ModbusUtils.splicing(port, "05", ParseSystemUtil.decToHex(num), "0000", false);
    }


    //

    public String closeA_DAM() {
        return "010F00000010020000E220";
    }
    public String closeB_DAM() {
        return "010F00100010020000E0B0";
    }
}
